import { getToken, removeToken, setToken } from '@/utils/auth'
import { getInfo, getUserStaffPhoto, login } from '@/api/user'

export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      setToken(token)
    },
    setUserInfo(state, userinfo) {
      state.userInfo = userinfo
    },
    removeToken(state) {
      state.token = ''
      removeToken()
    },
    removeUserInfo(state) {
      state.userinfo = {}
    }
  },
  actions: {
    async postToken(context, payload) {
      const res = await login(payload)
      context.commit('setToken', res.data)
      return res
    },
    async getUserInfo(context) {
      const res = await getInfo()
      // console.log(res)
      const res1 = await getUserStaffPhoto(res.data.userId)
      // console.log(res1)
      // context.commit('setUserInfo', res.data)
      context.commit('setUserInfo', { ...res.data, ...res1.data })
      // console.log(res, res1)
      return { ...res.data, ...res1.data }
    },
    logout(context) {
      context.commit('removeToken')
      context.commit('removeUserInfo')
    }
  }
}
